package net.jsoft.platform.monitor.api;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sun.management.OperatingSystemMXBean;

import io.swagger.annotations.ApiOperation;
import net.jsoft.platform.monitor.entity.MonitorInfo;
import net.jsoft.platform.monitor.util.ComputerMonitorUtil;

@RestController
@RequestMapping(value = "/monitor")
public class MonitorAPI
{

    @ApiOperation(value = "信息监控", notes = "提供服务器信息")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public MonitorInfo getInfo() throws Exception
    {

        MonitorInfo info = getMonitorInfoBean();
        return info;

    }

    @ApiOperation(value = "信息监控CPU性能", notes = "提供服务器CPU信息")
    @RequestMapping(value = "/cpuRatio", method = RequestMethod.POST)
    public double cpuRatio() throws Exception
    {
        double cpuUsage = ComputerMonitorUtil.getCpuUsage();
        System.out.println(cpuUsage);
        return cpuUsage;

    }

    @ApiOperation(value = "信息监控总线程数", notes = "提供服务器总线程数")
    @RequestMapping(value = "/totalThread", method = RequestMethod.POST)
    public double totalThread() throws Exception
    {
        // 获得线程总数   
        ThreadGroup parentThread;
        for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent())
            ;
        int totalThread = parentThread.activeCount();
        return totalThread;

    }

    @ApiOperation(value = "信息监控内存信息", notes = "提供服务器内存信息")
    @RequestMapping(value = "/memUsage", method = RequestMethod.POST)
    public double memUsage() throws Exception
    {
        double memUsage = ComputerMonitorUtil.getMemUsage();
        return memUsage;

    }

    @ApiOperation(value = "信息监控磁盘信息", notes = "提供服务器磁盘信息")
    @RequestMapping(value = "/diskUsage", method = RequestMethod.POST)
    public double diskUsage() throws Exception
    {
        double diskUsage = ComputerMonitorUtil.getDiskUsage();
        return diskUsage;

    }

    public MonitorInfo getMonitorInfoBean() throws Exception
    {
        double mb = 1024 * 1024 * 1.0;
        double gb = 1024 * 1024 * 1024 * 1.0;

        // jvm
        double totalMemory = Runtime.getRuntime().totalMemory() / mb;
        double freeMemory = Runtime.getRuntime().freeMemory() / mb;
        double maxMemory = Runtime.getRuntime().maxMemory() / mb;
        // os
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        String osName = System.getProperty("os.name");
        String osArch = System.getProperty("os.arch");
        String osVersion = System.getProperty("os.version");
        String javaName = System.getProperty("java.specification.name");
        String javaVendor = System.getProperty("java.specification.vendor");
        String javaVersion = System.getProperty("java.specification.version");
        double totalMemorySize = osmxb.getTotalPhysicalMemorySize() / gb;
        //        double freeMemorySize = osmxb.getFreePhysicalMemorySize() / gb;
        //        double usedMemorySize = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / gb;

        // MonitorInfo
        MonitorInfo infoBean = new MonitorInfo();
        infoBean.setTotalMemory(getIntValue(totalMemory));
        infoBean.setFreeMemory(getIntValue(freeMemory));
        infoBean.setMaxMemory(getIntValue(maxMemory));
        infoBean.setOsName(osName);
        infoBean.setOsArch(osArch);
        infoBean.setOsVersion(osVersion);
        infoBean.setTotalMemorySize(getIntValue(totalMemorySize));
        //        infoBean.setFreeMemorySize(getIntValue(freeMemorySize));
        //        infoBean.setUsedMemorySize(getIntValue(usedMemorySize));
        infoBean.setProcessors(Runtime.getRuntime().availableProcessors());
        infoBean.setJavaName(javaName);
        infoBean.setJavaVendor(javaVendor);
        infoBean.setJavaVersion(javaVersion);
        //        infoBean.setCpuRatio(cpuRatio);
        //        infoBean.setTotalThread(totalThread);
        return infoBean;
    }

    /**
     * 四舍五入取整
     * 
     * @param d
     * @return
     */
    private static int getIntValue(double d)
    {
        return new BigDecimal(d).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    }

}
